define(function() {
    var datagrid;
    var toolbar;
    
    function handleUserQueryParams() {
        var qparams = toolbar.find('#searchUser').serializeObject();
        if (qparams.cardCode) {
            $('[comboname=userType]').combobox('setValue', 2);
            qparams.userType = 2;
        }
        if (qparams.deviceCode) {
            $('[comboname=userType]').combobox('setValue', 9);
            qparams.userType = 9;
        }
        if (qparams.userType == 9) {
            datagrid.datagrid('hideColumn', 'cardCode');
            datagrid.datagrid('showColumn', 'deviceCode');
        } else if (qparams.userType == 2) {
            datagrid.datagrid('showColumn', 'cardCode');
            datagrid.datagrid('hideColumn', 'deviceCode');
        } else {
            datagrid.datagrid('hideColumn', 'cardCode');
            datagrid.datagrid('hideColumn', 'deviceCode');
        }
        
        return qparams;
    }
    
    function telphoneIsRequired(userType) {
        return [0, 1].indexOf(+userType) > -1;
    }
    function imageIsOptional(userType) {
        return [0, 1].indexOf(+userType) > -1;
    }

    return function(viewType) {
        toolbar = $('#userGridToolbar' + viewType);
        datagrid = $('#userGrid' + viewType);

        var userTypes = [0, 1, 2, 9];       //限制用户类型们
        var allOptionValue = -1;//表示 全部 的值;
        switch (+viewType) {
        case 0:
            break;
        case 1://关爱设置
            userTypes = [1, 9];
            allOptionValue = -3;
            break;
        case 2:
            userTypes = [1,9];
            allOptionValue = -3;
            break;
            break;
        }

        datagrid.datagrid({
            url: CONFIG.baseUrl + 'user/listUser.do',
            queryParams: {userType: allOptionValue},
            onSelect: function(i, user) {
                //特定用户类型特有的功能表
                var userTypeFuncCodeMap = {
                    0: ['user-resetPwd'],
                    1: ['user-resetPwd'],
                    2: ['changeCard'],
                    9: ['device-update','device-changeDeviceCode','device-powerOff','device-cancel','device-remoteCall','device-detail']
                }
                _.flatten(_.values(userTypeFuncCodeMap)).forEach(function(id) {
                    $('#' + id).hide();
                });
                userTypeFuncCodeMap[user.userType].forEach(function(id) {
                    $('#' + id).show();
                });
            }
        });
        
        toolbar.find('[comboname=userType]').comboboxFromDict({
            dict: DictMan.items('user.type').filter(function(t) { return userTypes.indexOf(+t.value) > -1 }),
            emptyItem: new DictItem(allOptionValue, '全部'),
            value: allOptionValue,
            onSelect: function(item) {
                var userType = +item.value;
                switch (userType) {
                case -1:
                case 0:
                case 1:
                    $('[textboxname=deviceCode]').textbox('clear');
                    $('[textboxname=cardCode]').textbox('clear');
                    break;
                case 2:
                    $('[textboxname=deviceCode]').textbox('clear');
                    break;
                case 9:
                    $('[textboxname=cardCode]').textbox('clear');
                    break;
                }
            }
        });
        var lastQueryParams = {};
        initUserQuerier($('#userQuerier', toolbar), '#searchUser', datagrid, function(params) {
            lastQueryParams = params;
        }, { allUserTypeValue: -3 });
        
    }
});